package com.beiding.listener;

import com.beiding.dao.OrderDao;
import com.beiding.dao.SettingsDao;
import com.beiding.pojo.order.Order;
import com.beiding.service.impl.MessageSender;
import com.beiding.timeout.bean.TimeShaft;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Pay extends TimeoutListener {


    protected Pay(OrderDao orderDao, TimeShaft<String> timeShaft) {
        super(orderDao, timeShaft, "pay");
    }

    private SettingsDao settings;

    @Autowired
    public void setSettings(SettingsDao settings) {
        this.settings = settings;
    }


    private MessageSender sender;

    @Autowired
    public void setSender(MessageSender sender) {
        this.sender = sender;
    }

    @Override
    public void run(String id) {
        Order order = orderDao.getInsensitive(id);
        if (judge(order, Order.CREATE)) {
            orderDao.updateField(order.getId(), "state", Order.TERMINATION);

            //延时删除
            timeShaft.delay("delete", settings.getTimeout("delete"), id);

            order.setState(Order.TERMINATION);

            sender.sendToTenant(order.getHolderId(),update(order).msg("订单超时未付款,已经失效"));
        }
    }
}

